﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RpgXna.Model.GameManager;
using RpgXna.Model.Helpers;
using RpgXna.Model.Interfaces.InterfacesManagement;
using RpgXna.Model.Interfaces.MainMenu.MenuBehaviors;
using RpgXna.Model.Menu.MenuBehaviors;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna.Model.Interfaces.MainMenu
{
    public class Menu : IInterface
    {
        private readonly float _centerScreen;
        private readonly GraphicsDeviceManager _graphics;
        private readonly GameStateManager _gsm;
        private readonly IInterfaceComponent _interface;
        private readonly List<MenuButton> _menuButtons;
        private readonly MenuShowHideBehavior _showHide;

        private Texture2D _layoutText;

        public Menu(GraphicsDeviceManager graphics, IInterfaceComponent ifc, GameStateManager gsm, InputManager input)
        {
            _graphics = graphics;

            _menuButtons = new List<MenuButton>();
            _interface = ifc;
            _gsm = gsm;

            _centerScreen = (float)_graphics.GraphicsDevice.Viewport.Width / 2;
            input.LeftMouseButtonClick += InputLeftMouseButtonClick;
            _showHide = new MenuShowHideBehavior(() => _gsm.State != GameStates.NotStarted, ShowHide);
        }

        public IInterfaceComponent Interface
        {
            get { return _interface; }
        }

        public void Draw(SpriteBatch sb)
        {
            if (!Interface.IsVisible) return;

            sb.Draw(_layoutText, new Vector2(_centerScreen - (float)_layoutText.Width / 2, 0), Color.White);
            foreach (MenuButton mb in _menuButtons)
            {
                mb.Draw(sb);
            }
        }

        public void Load(ContentManager content)
        {
            _layoutText = content.Load<Texture2D>("menu_layout");

            _menuButtons.AddRange(new[]
                                      {
                                          new MenuButton(content.Load<Texture2D>("menu_new"), _centerScreen, 120,
                                                         new NewGameBehavior(_gsm)),
                                          new MenuButton(content.Load<Texture2D>("menu_charger"), _centerScreen, 215),
                                          new MenuButton(content.Load<Texture2D>("menu_save"), _centerScreen, 300),
                                          new MenuButton(content.Load<Texture2D>("menu_quit"), _centerScreen, 380,
                                                         new ExitGameBehavior(_gsm))
                                      });
        }

        public void Update(IKeyboardStateWrapper kbState)
        {
            ManageTabUserKey(kbState);
        }

        private void InputLeftMouseButtonClick(MouseState state)
        {
            if (!Interface.IsVisible) return;
            foreach (MenuButton mb in _menuButtons)
            {
                if (mb.Box.Contains(state.X, state.Y))
                {
                    mb.DoAction();
                    ShowHide();
                    break;
                }
            }
        }

        private void ManageTabUserKey(IKeyboardStateWrapper kbState)
        {
            _showHide.Execute(kbState);
        }

        private void ShowHide()
        {
            if (Interface.IsVisible)
                Interface.Hide();
            else
            {
                Interface.Show();
            }
        }

        public bool IsVisible
        {
            get { return _interface.IsVisible; }
        }

        public int Priority
        {
            get { return _interface.Priority; }
        }
        public void Show()
        {
            _interface.Show();
        }

        public void Hide()
        {
            _interface.Hide();
        }


    }
}